const http = require('http');
const fs = require('fs');
const path = require('path');

const server = http.createServer();
server.on('request', (req, res) => {
    res.setHeader('Content-Type', 'text/html;charset=utf-8')
        // url 地址为 /clok/about.hthml
    const url = req.url;

    //映射地址，把url地址 改为文件地址
    //  const fpath = path.join(__dirname, url);

    //优化路径 当用户请求根路径 比如 12.0.0.1 就返回 index.html

    let fpath = ''
    if (url == '/') {
        fpath = path.join(__dirname, './clock/index.html')

    } else {
        //当用户 直接输入 127.0.0.1/index.html 也能请求   
        fpath = path.join(__dirname, '/clock', url)
    }
    fs.readFile(fpath, 'utf-8', (error, datastr) => {
        if (error) return res.end('404读取失败');
        res.end(datastr)
    })

});
server.listen(80, () => {
    console.log('Server running at http://127.0.0.1');
})